﻿using NPOI.SS.UserModel;
using Org.BouncyCastle.Tls;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.DeviceStatus
{
    public class ColumnThreeUCViewModel : BindableBase
    {
        /// <summary>
        /// 构造器
        /// </summary>
        public ColumnThreeUCViewModel()
        {
            // 初始化调度程序定时器
            InitTimer();

            // 初始化设备
            InitDevice();

        }


        #region 时间显示
        /// <summary>
        /// 定义调度程序定时器
        /// </summary>
        private DispatcherTimer _timer = new DispatcherTimer();

        /// <summary>
        /// 初始化调度程序定时器
        /// </summary>
        private void InitTimer()
        {
            // 初始化调度程序定时器
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }

        /// <summary>
        /// 时间刷新
        /// </summary>
        private void Timer_Tick(object sender, EventArgs e)
        {
            
        }


        /// <summary>
        /// 初始化设备
        /// </summary>
        private void InitDevice()
        {
            // 网络状态
            NetworkStateCompute();

            // 显示器信息
            DisplayInformationCompute();

            // BIOS信息
            BIOSInfoCompute();
        }
        #endregion


        #region 网络状态

        /// <summary>
        /// 网卡名称
        /// </summary>
        private string _NicName;

        /// <summary>
        /// 网卡名称
        /// </summary>
        public string NicName
        {
            get { return _NicName; }
            set { 
                _NicName = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 状态
        /// </summary>
        private string _Status;

        /// <summary>
        /// 状态
        /// </summary>
        public string Status
        {
            get { return _Status; }
            set { 
                _Status = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// IPv4地址
        /// </summary>
        private string _IPv4Address;

        /// <summary>
        /// IPv4地址
        /// </summary>
        public string IPv4Address
        {
            get { return _IPv4Address; }
            set { 
                _IPv4Address = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 子网掩码
        /// </summary>
        private string _SubnetMask;

        /// <summary>
        /// 子网掩码
        /// </summary>
        public string SubnetMask
        {
            get { return _SubnetMask; }
            set { 
                _SubnetMask = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 默认网关
        /// </summary>
        private string _DefaultGateway;

        /// <summary>
        /// 默认网关
        /// </summary>
        public string DefaultGateway
        {
            get { return _DefaultGateway; }
            set { 
                _DefaultGateway = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// DNS服务器
        /// </summary>
        private string _DNSServer;

        /// <summary>
        /// DNS服务器
        /// </summary>
        public string DNSServer
        {
            get { return _DNSServer; }
            set { 
                _DNSServer = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 网络状态
        /// </summary>
        private void NetworkStateCompute()
        {
            try
            {
                // 获取所有网络适配器
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

                foreach (NetworkInterface ni in interfaces)
                {
                    NicName = ni.Name;
                    Status = ni.OperationalStatus.ToString();

                    if (ni.OperationalStatus == OperationalStatus.Up)
                    {
                        IPInterfaceProperties ipProps = ni.GetIPProperties();

                        // 获取IPv4地址
                        foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                IPv4Address = ip.Address.ToString();
                                SubnetMask = ip.IPv4Mask.ToString();
                            }
                        }

                        // 获取默认网关
                        foreach (GatewayIPAddressInformation gateway in ipProps.GatewayAddresses)
                        {
                            DefaultGateway = gateway.Address.ToString();
                        }

                        // 获取DNS服务器
                        foreach (var dns in ipProps.DnsAddresses)
                        {
                            DNSServer = dns.ToString();
                        }
                    }

                    Console.WriteLine(new string('-', 40));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误: " + ex.Message);
            }
        }

        #endregion


        #region 显示器信息

        /// <summary>
        /// 显示器名称
        /// </summary>
        private string _DisplayName;

        /// <summary>
        /// 显示器名称
        /// </summary>
        public string DisplayName
        {
            get { return _DisplayName; }
            set { 
                _DisplayName = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 带宽
        /// </summary>
        private string _Bandwidth;

        /// <summary>
        /// 带宽
        /// </summary>
        public string Bandwidth
        {
            get { return _Bandwidth; }
            set {
                _Bandwidth = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 显示器状态
        /// </summary>
        private string _DisplayState;

        /// <summary>
        /// 显示器状态
        /// </summary>
        public string DisplayState
        {
            get { return _DisplayState; }
            set {
                _DisplayState = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 设备ID
        /// </summary>
        private string _DeviceID;

        /// <summary>
        /// 设备ID
        /// </summary>
        public string DeviceID
        {
            get { return _DeviceID; }
            set {
                _DeviceID = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 制造商
        /// </summary>
        private string _Manufacturer;

        /// <summary>
        /// 制造商
        /// </summary>
        public string Manufacturer
        {
            get { return _Manufacturer; }
            set {
                _Manufacturer = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 显示器信息
        /// </summary>
        private void DisplayInformationCompute()
        {
            try
            {
                // 查询显示器信息
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");

                Console.WriteLine("=== 显示器信息 ===");
                foreach (ManagementObject monitor in searcher.Get())
                {
                    DisplayName = monitor["Caption"].ToString();
                    DisplayState = monitor["Status"].ToString();
                }

                // 查询 PnP 信息
                ManagementObjectSearcher pnpSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{4d36e96e-e325-11ce-bfc1-08002be10318}'");

                Console.WriteLine("=== 显示器 PnP 信息 ===");
                foreach (ManagementObject pnp in pnpSearcher.Get())
                {
                    DeviceID = pnp["DeviceID"].ToString();
                    Manufacturer = pnp["Manufacturer"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误: " + ex.Message);
            }
        }

        #endregion


        #region  BIOS信息

        /// <summary>
        /// Bios制造商
        /// </summary>
        private string _BiosManufacturer;

        /// <summary>
        /// Bios制造商
        /// </summary>
        public string BiosManufacturer
        {
            get { return _BiosManufacturer; }
            set { 
                _BiosManufacturer = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Bios版本
        /// </summary>
        private string _BiosVersion;

        /// <summary>
        /// Bios版本
        /// </summary>
        public string BiosVersion
        {
            get { return _BiosVersion; }
            set { 
                _BiosVersion = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Bios识别号
        /// </summary>
        private string _BiosIdNumber;

        /// <summary>
        /// Bios识别号
        /// </summary>
        public string BiosIdNumber
        {
            get { return _BiosIdNumber; }
            set { 
                _BiosIdNumber = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Bios发布日期
        /// </summary>
        private string _BiosReleaseDate;

        /// <summary>
        /// Bios发布日期
        /// </summary>
        public string BiosReleaseDate
        {
            get { return _BiosReleaseDate; }
            set { 
                _BiosReleaseDate = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Bios序列号
        /// </summary>
        private string _BiosSerialNumber;

        /// <summary>
        /// Bios序列号
        /// </summary>
        public string BiosSerialNumber
        {
            get { return _BiosSerialNumber; }
            set { 
                _BiosSerialNumber = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Bios名称
        /// </summary>
        private string _BiosName;

        /// <summary>
        /// Bios名称
        /// </summary>
        public string BiosName
        {
            get { return _BiosName; }
            set { 
                _BiosName = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// BIOS信息
        /// </summary>
        private void BIOSInfoCompute()
        {
            try
            {
                // 查询 Win32_BIOS 类以获取 BIOS 信息
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");

                foreach (ManagementObject bios in searcher.Get())
                {
                    BiosManufacturer = bios["Manufacturer"].ToString();
                    BiosVersion = bios["SMBIOSBIOSVersion"].ToString();
                    //BiosIdNumber = bios["IdentificationCode"].ToString();
                    BiosReleaseDate = Convert.ToDateTime(bios["ReleaseDate"].ToString()).ToString("yyyy-MM-dd");
                    BiosSerialNumber = bios["SerialNumber"].ToString();
                    BiosName = bios["Name"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取 BIOS 信息时出错: " + ex.Message);
            }
        }


        #endregion
    }
}
